package com.superhero.website.controller;

import cn.hutool.poi.excel.ExcelWriter;
import com.superhero.common.pojo.ApiResult;
import com.superhero.common.pojo.PageInput;
import com.superhero.common.pojo.vo.PageVo;
import com.superhero.common.pojo.vo.website.HouseholdHealthInfo;
import com.superhero.common.utils.ExcelUtils;
import com.superhero.website.service.HouseholdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;


@Api(tags = {"后台管理系统"})
@RestController
@RequestMapping("/website/household")
public class HouseholdController {

    @Autowired
    HouseholdService householdService;

    @ApiOperation(value = "查询住户信息详情")
    @GetMapping("/get")
    private ApiResult<HouseholdHealthInfo> getDetail(@NotNull Integer id) {
        HouseholdHealthInfo vo = householdService.getDetail(id);
        return ApiResult.success(vo);
    }

    @ApiOperation(value = "列表显示住户（分页）", notes = "key可为人员用户名、姓名、手机号，地址，健康状态（正常/异常/确诊）的全模糊查询")
    @GetMapping("")
    private ApiResult<PageVo> showList(@Valid PageInput queryInput, String keyword, @NotNull @ApiParam(name = "0-未认证 1=已认证 2-认证失败", required = true) Integer personType) {
        return ApiResult.success(householdService.showList(queryInput, keyword, personType));
    }

    @ApiOperation(value = "导出住户列表")
    @GetMapping("/export")
    private void exportList(String keyword, @NotNull(message = "personType") @ApiParam(name = "0-未认证 1=已认证 2-认证失败", required = true) Integer personType, HttpServletResponse response) {
        ExcelWriter excelWriter = householdService.exportList(keyword, personType, response);
        if (null != excelWriter) {
            // 进行 excel 写出操作。
            try {
                ExcelUtils.responseExcel("住户列表", response, excelWriter);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @ApiOperation(value = "住户确诊情况更改")
    @PutMapping("/updateConfirmedStatus")
    private ApiResult<String> updateConfirmedStatus(@NotNull Integer id) {
        householdService.updateConfirmedStatus(id);
        return ApiResult.success();
    }

    @ApiOperation(value = "锁定/解锁住户")
    @DeleteMapping("")
    private ApiResult<String> delete(@NotNull Integer id) {
        householdService.delete(id);
        return ApiResult.success();
    }

    @ApiOperation(value = "用户认证")
    @GetMapping("/verify")
    private ApiResult<String> verify(@NotNull Integer id, @NotNull(message = "0-不通过 1-通过") Integer pass) {
        householdService.verify(id, pass);
        return ApiResult.success();
    }
}
